/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package decore.database;

import decore.core.DBConnection;
import decore.entidade.Material;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dola
 */
public class ManangerMaterial implements ManangerEntidade {

    @Override
    public boolean cadastrar(Object object) {
        Material material = (Material) object;
        PreparedStatement statement;
        String sql = "INSERT INTO material (nome) values (?)";
        try {
            statement = DBConnection.getConnection().prepareStatement(sql);
            statement.setString(1, material.getNome());
            return statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ManangerMaterial.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

    }

    @Override
    public boolean atualizar(Object object) {
        Material material = (Material) object;
        PreparedStatement statement;
        String sql = "UPDATE material SET (nome,ativo) values (?,?) WHERE idMaterial=?";
        try {
            statement = DBConnection.getConnection().prepareStatement(sql);
            statement.setString(1, material.getNome());
            statement.setBoolean(2, material.isAtivo());
            statement.setLong(3, material.getCodigo());
            return statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ManangerMaterial.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

    }

    @Override
    public boolean excluir(Object object) {
        Material material = (Material) object;
        PreparedStatement statement;
        String sql = "UPDATE material SET (excluido) VALUES (?) WHERE idMaterial=?";
        try {
            statement = DBConnection.getConnection().prepareStatement(sql);
            statement.setInt(1, 1);
            statement.setLong(2, material.getCodigo());
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(ManangerMaterial.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

    }

    @Override
    public Object[] pesquisar(String pesquisa) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public Object[] pesquisar(Long id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object[] pesquisar(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object getEntidade(Long id) {
        Material material = null;
        PreparedStatement statement;
        ResultSet resultset;
        String sql = "SELECT * FROM material WHERE idMaterial=? AND excluido !=1";
        try {
            statement = DBConnection.getConnection().prepareStatement(sql);
            statement.setLong(1, id);
            resultset = statement.executeQuery();
            if (resultset.next()) {
                material = new Material(resultset.getLong("idMaterial"), resultset.getString("nome"));
                return material;
            } else {
                return null;
            }
        } catch (SQLException ex) {
            Logger.getLogger(ManangerMaterial.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }


    }    
}
